from sqlalchemy import Integer, String, Text
from sqlalchemy.orm import Mapped, relationship
from sqlalchemy.orm import mapped_column

from .engine import Base


class Chapter(Base):
    __tablename__ = 'chapter'
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    book_id: Mapped[int] = mapped_column(Integer, comment="书籍ID")
    name: Mapped[str] = mapped_column(String(64), comment="章节名")
    content: Mapped[str | None] = mapped_column(Text, comment="书籍描述信息")
    sort: Mapped[int] = mapped_column(Integer, comment="章节排序")
    url: Mapped[str] = mapped_column(String(1024), unique=True, comment="爬虫获取的链接")
    page_source: Mapped[str | None] = mapped_column(Text, comment="网页源代码")
    book: Mapped["Book"] = relationship("Book", primaryjoin="Book.id == Chapter.book_id", back_populates="chapters")
